package com.lvmama.service.rhino.flow.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;
import com.lvmama.dao.rhino.flow.FlowConversionDao;
import com.lvmama.pojo.dto.rhino.flow.FlowConversionDTO;
import com.lvmama.pojo.dto.rhino.flow.FlowStatisticsFunnel;
import com.lvmama.pojo.dto.rhino.page.FlowStatisticsVo;
import com.lvmama.pojo.entity.rhino.page.FlowData;
import com.lvmama.service.rhino.flow.FlowConversionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : FlowConversionServiceImpl
 * @Description : 流量转化实现类
 * @Author :Administrator
 * @Date : 2018/9/3 17:39
 * @Version V1.0
 **/
@Slf4j
@Service
public class FlowConversionServiceImpl implements FlowConversionService {
    @Autowired
    FlowConversionDao flowConversionDao;

    @Override
    public Result<List<FlowStatisticsVo>> queryFlowbyApp(FlowConversionDTO flowConversionDTO) {
        log.info("FlowConversionServiceImpl queryFlowbyApp start");
        List<FlowStatisticsVo> flowList = null;
        try{
            flowList = flowConversionDao.queryFlowbyApp(flowConversionDTO);
        }catch (Exception e){
            log.error("FlowConversionServiceImpl queryFlowbyApp error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl queryFlowbyApp end");
        return Result.success(flowList);
    }

    @Override
    public Result<List<FlowStatisticsVo>> queryFlowbyChannelWap(FlowConversionDTO flowConversionDTO) {
        log.info("FlowConversionServiceImpl queryFlowChaneelwap start");
        List<FlowStatisticsVo> flowList= new ArrayList<>();

        try{
            flowList = flowConversionDao.queryFlowbyChannelWap(flowConversionDTO);
        }catch(Exception e){
            log.error("FlowConversionServiceImpl queryFlowChaneelwap error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        return Result.success(flowList);
    }

    @Override
    public Result<List<FlowStatisticsVo>> queryFlowbyChannel(FlowConversionDTO flowConversionDTO) {
        log.info("FlowConversionServiceImpl queryFlowbyChannel start");
        List<FlowStatisticsVo> flowStatisticsVoList = null;
        try{
            flowStatisticsVoList = flowConversionDao.queryFlowbyChannel(flowConversionDTO);
        }catch (Exception e){
            log.error("FlowConversionServiceImpl queryFlowbyChannel error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl queryFlowbyChannel end");
        return Result.success(flowStatisticsVoList);
    }

    @Override
    public Result<PageInfo<FlowStatisticsFunnel>> queryAllFunnel(FlowStatisticsFunnel flowStatisticsFunnel) {
        log.info("FlowConversionServiceImpl queryAllFunnel start");
        PageHelper.startPage(flowStatisticsFunnel.getPage(), flowStatisticsFunnel.getPageSize());
        PageInfo<FlowStatisticsFunnel> page = null;
        try{
            page = new PageInfo<>(flowConversionDao.queryAllFunnel(flowStatisticsFunnel));
        }catch (Exception e){
            log.error("FlowConversionServiceImpl queryAllFunnel error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl queryAllFunnel end");
        return Result.success(page);
    }

    @Override
    public Result<List<FlowStatisticsFunnel>> queryFunnelDetail(FlowStatisticsFunnel funnel) {
        log.info("FlowConversionServiceImpl queryFunnelDetail start");
        List<FlowStatisticsFunnel> flowList = null;
        try{
            flowList = flowConversionDao.queryFunnelDetail(funnel);
        }catch (Exception e){
            log.error("FlowConversionServiceImpl queryFunnelDetail error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl queryFunnelDetail end");
        return Result.success(flowList);
    }

    @Override
    public Result<FlowData> queryFlowStatisticsData(FlowConversionDTO flowConversionDTO) {
        log.info("FlowConversionServiceImpl queryFlowStatisticsData start");
        FlowData flowData = null;
        try{
            flowData = flowConversionDao.queryFlowStatisticsData(flowConversionDTO);
        }catch (Exception e){
            log.error("FlowConversionServiceImpl queryFlowStatisticsData error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl queryFlowStatisticsData end");
        return Result.success(flowData);
    }

    @Override
    public void updateFunnel(FlowStatisticsFunnel funnel) {
        flowConversionDao.updateFunnel(funnel);
    }

    @Override
    public Result<FlowStatisticsFunnel> selectFunnel(FlowStatisticsFunnel funnel) {
        log.info("FlowConversionServiceImpl selectFunnel start");
        FlowStatisticsFunnel funnelData = null;
        try{
            funnelData = flowConversionDao.selectFunnel(funnel);
        }catch (Exception e){
            log.error("FlowConversionServiceImpl selectFunnel error:{}",e.getMessage());
            return Result.error(CodeMessage.SERVER_ERROR);
        }
        log.info("FlowConversionServiceImpl selectFunnel end");
        return Result.success(funnelData);
    }

    @Override
    public void deleteFunnelDetail(FlowStatisticsFunnel funnel) {
        flowConversionDao.deleteFunnelDetail(funnel);
    }

    @Override
    public void insertFunnel(FlowStatisticsFunnel funnel) {
        flowConversionDao.insertFunnel(funnel);
    }

    @Override
    public void insertFunnelDetail(FlowStatisticsFunnel funnel) {
        flowConversionDao.insertFunnelDetail(funnel);
    }

    @Override
    public void deleteFunnel(FlowStatisticsFunnel funnel) {
        flowConversionDao.deleteFunnel(funnel);
    }
}
